package com.learn.dict.conf.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.learn.dict.annotation.Dict;

import java.io.IOException;

/**
 * 功能描述: 字典序列化 <br/>
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/10/22 21:44
 * @updateUser: MI
 * @updateDate: 2023/10/22 21:44
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class DictSerializer extends StdSerializer<Object> {

    private final Dict dict;

    public DictSerializer(JavaType type, Dict dict) {
        super(type);
        this.dict = dict;
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(dict.re().getLabel(dict.type(), dict.separator(), value.toString()));
    }
}
